package com.ylsk.check.service.impl;

import java.util.Date;
import java.util.List;

import com.google.common.primitives.Longs;
import com.ylsk.check.domain.ReportInfo;
import com.ylsk.check.domain.dto.MsgTemplateDTO;
import com.ylsk.check.domain.emum.ApprStatusEnum;
import com.ylsk.check.domain.emum.MsgTemplateEnum;
import com.ylsk.check.domain.emum.ReportStatusEnum;
import com.ylsk.check.domain.vo.ReportApprLogVO;
import com.ylsk.check.mapper.ReportInfoMapper;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.ReportApprLogMapper;
import com.ylsk.check.domain.ReportApprLog;
import com.ylsk.check.service.IReportApprLogService;

/**
 * 报备信息审批日志Service业务层处理
 *
 * @author yangsy
 * @date 2023-12-15
 */
@Service
public class ReportApprLogServiceImpl implements IReportApprLogService
{
    @Autowired
    private ReportApprLogMapper reportApprLogMapper;

    @Autowired
    private ReportInfoMapper reportInfoMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private ReportInfoHelper helper;

    /**
     * 查询报备信息审批日志
     *
     * @param reportApprLogId 报备信息审批日志主键
     * @return 报备信息审批日志
     */
    @Override
    public ReportApprLog selectReportApprLogByReportApprLogId(Long reportApprLogId)
    {
        return reportApprLogMapper.selectReportApprLogByReportApprLogId(reportApprLogId);
    }

    /**
     * 查询报备信息审批日志列表
     *
     * @param reportApprLog 报备信息审批日志
     * @return 报备信息审批日志
     */
    @Override
    public List<ReportApprLogVO> selectReportApprLogList(ReportApprLog reportApprLog)
    {
        return reportApprLogMapper.selectReportApprLogList(reportApprLog);
    }

    /**
     * 新增报备信息审批日志
     *
     * @param reportApprLog 报备信息审批日志
     * @return 结果
     */
    @Override
    public int insertReportApprLog(ReportApprLog reportApprLog)
    {
        //项目报备单id
        final Long reportInfoId = reportApprLog.getReportInfoId();
        if(null == reportInfoId){
            throw new ServiceException("项目报备单号不能为空!");
        }
        //审批状态
        Integer apprStatus = reportApprLog.getApprStatus();
        if(null == apprStatus){
            throw new ServiceException("审批状态不能为空!");
        }
        ReportInfo reportInfo = reportInfoMapper.selectById(reportInfoId);
        if(null == reportInfo){
            throw new ServiceException(String.format("业务数据不存在,reportInfoId=%1$s", reportInfoId));
        }
        //提取上次更新人
        Long updateBy = reportInfo.getUpdateBy();
        //提取上次更新时间
        Date updateTime = reportInfo.getUpdateTime();

        long reportApprLogId = snowIdConfig.getSnowId();
        reportApprLog.setReportApprLogId(reportApprLogId);
        reportApprLog.setSubmitUser(updateBy);
        reportApprLog.setSubmitTime(updateTime);
        reportApprLog.setApprTime(new Date());
        reportApprLog.setApprUserId(SecurityUtils.getUserId());

        //报备信息提交之后 还需要更新报备信息表的状态
        final String p1 = reportInfo.getProjectName();
        MsgTemplateDTO msgTemplateDTO = new MsgTemplateDTO(SecurityUtils.getUserId(), Longs.asList(reportInfo.getCreateBy()));
        if(apprStatus == ApprStatusEnum.PASS.getCode()){
            reportInfo.setReportStatus(ReportStatusEnum.PASS.getCode());
            msgTemplateDTO.setMsgTemplateEnum(MsgTemplateEnum.TEMPLATE_SGBB_PASS);
            helper.saveMsg(msgTemplateDTO,p1);
        }else{
            reportInfo.setReportStatus(ReportStatusEnum.REJECT.getCode());
            msgTemplateDTO.setMsgTemplateEnum(MsgTemplateEnum.TEMPLATE_SGBB_REJECT);
            helper.saveMsg(msgTemplateDTO,p1);
        }
        //  记录最近一次审批id
        reportInfo.setReportApprLogId(reportApprLogId);
        int result = 0 ;
        result += reportApprLogMapper.insertReportApprLog(reportApprLog);
        result += reportInfoMapper.updateById(reportInfo);
        return result;
    }

    /**
     * 修改报备信息审批日志
     *
     * @param reportApprLog 报备信息审批日志
     * @return 结果
     */
    @Override
    public int updateReportApprLog(ReportApprLog reportApprLog)
    {
        reportApprLog.setUpdateTime(DateUtils.getNowDate());
        return reportApprLogMapper.updateReportApprLog(reportApprLog);
    }

    /**
     * 批量删除报备信息审批日志
     *
     * @param reportApprLogIds 需要删除的报备信息审批日志主键
     * @return 结果
     */
    @Override
    public int deleteReportApprLogByReportApprLogIds(Long[] reportApprLogIds)
    {
        return reportApprLogMapper.deleteReportApprLogByReportApprLogIds(reportApprLogIds);
    }

    /**
     * 删除报备信息审批日志信息
     *
     * @param reportApprLogId 报备信息审批日志主键
     * @return 结果
     */
    @Override
    public int deleteReportApprLogByReportApprLogId(Long reportApprLogId)
    {
        return reportApprLogMapper.deleteReportApprLogByReportApprLogId(reportApprLogId);
    }
}
